package com.example.testapp.data.entity

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.Index
import java.time.LocalDateTime

/**
 * 账单处理记录表
 */
@Entity(
    tableName = "balance_process_record",
    foreignKeys = [
        ForeignKey(
            entity = Balance::class,
            parentColumns = ["id"],
            childColumns = ["balance_id"],
            onDelete = ForeignKey.CASCADE
        )
    ],
    indices = [
        Index(name = "idx_balance_process_record_balance_id", value = ["balance_id"]),
        Index(name = "idx_balance_process_record_status", value = ["status"]),
        Index(name = "idx_balance_process_record_del_flag", value = ["del_flag"])
    ]
)
class BalanceProcessRecord : BaseEntity() {

    /**
     * 关联账单ID
     */
    @JvmField
    @ColumnInfo(name = "balance_id")
    var balanceId: Long? = null

    /**
     * 处理类型（1-OCR识别，2-图片存储，3-缩略图生成，4-信息提取）
     */
    @JvmField
    @ColumnInfo(name = "process_type")
    var processType: Int? = null

    /**
     * 处理状态（1-待处理，2-处理中，3-已完成，4-失败）
     */
    @JvmField
    @ColumnInfo(name = "status")
    var status: Int? = null

    /**
     * 当前重试次数
     */
    @JvmField
    @ColumnInfo(name = "retry_count")
    var retryCount: Int? = null

    /**
     * 最大重试次数（默认3次）
     */
    @JvmField
    @ColumnInfo(name = "max_retry_count")
    var maxRetryCount: Int? = null

    /**
     * 处理开始时间
     */
    @JvmField
    @ColumnInfo(name = "start_time")
    var startTime: LocalDateTime? = null

    /**
     * 处理结束时间
     */
    @JvmField
    @ColumnInfo(name = "end_time")
    var endTime: LocalDateTime? = null

    /**
     * 计划下次重试时间
     */
    @JvmField
    @ColumnInfo(name = "next_retry_time")
    var nextRetryTime: LocalDateTime? = null

    /**
     * 处理耗时，毫秒
     */
    @JvmField
    @ColumnInfo(name = "time_consumed")
    var timeConsumed: Int? = null

    /**
     * 错误代码
     */
    @JvmField
    @ColumnInfo(name = "error_code")
    var errorCode: String? = null

    /**
     * 错误详情
     */
    @JvmField
    @ColumnInfo(name = "error_message")
    var errorMessage: String? = null

    /**
     * 错误类型
     */
    @JvmField
    @ColumnInfo(name = "error_type")
    var errorType: String? = null

    /**
     * 处理详情（JSON）
     */
    @JvmField
    @ColumnInfo(name = "process_details")
    var processDetails: String? = null

    /**
     * 设备信息
     */
    @JvmField
    @ColumnInfo(name = "device_info")
    var deviceInfo: String? = null

    /**
     * 网络类型
     */
    @JvmField
    @ColumnInfo(name = "network_type")
    var networkType: String? = null

    /**
     * 应用版本
     */
    @JvmField
    @ColumnInfo(name = "app_version")
    var appVersion: String? = null

    /**
     * WorkManager工作ID
     */
    @JvmField
    @ColumnInfo(name = "worker_id")
    var workerId: String? = null

    // Getter和Setter方法，用于Java代码兼容性
    fun getBalanceId(): Long? = balanceId
    fun setBalanceId(balanceId: Long?) { this.balanceId = balanceId }

    fun getProcessType(): Int? = processType
    fun setProcessType(processType: Int?) { this.processType = processType }

    fun getStatus(): Int? = status
    fun setStatus(status: Int?) { this.status = status }

    fun getRetryCount(): Int = retryCount ?: 0
    fun setRetryCount(retryCount: Int?) { this.retryCount = retryCount }

    fun getMaxRetryCount(): Int = maxRetryCount ?: 3
    fun setMaxRetryCount(maxRetryCount: Int?) { this.maxRetryCount = maxRetryCount }

    fun getStartTime(): LocalDateTime? = startTime
    fun setStartTime(startTime: LocalDateTime?) { this.startTime = startTime }

    fun getEndTime(): LocalDateTime? = endTime
    fun setEndTime(endTime: LocalDateTime?) { this.endTime = endTime }

    fun getNextRetryTime(): LocalDateTime? = nextRetryTime
    fun setNextRetryTime(nextRetryTime: LocalDateTime?) { this.nextRetryTime = nextRetryTime }

    fun getTimeConsumed(): Int? = timeConsumed
    fun setTimeConsumed(timeConsumed: Int?) { this.timeConsumed = timeConsumed }

    fun getErrorCode(): String? = errorCode
    fun setErrorCode(errorCode: String?) { this.errorCode = errorCode }

    fun getErrorMessage(): String? = errorMessage
    fun setErrorMessage(errorMessage: String?) { this.errorMessage = errorMessage }

    fun getErrorType(): String? = errorType
    fun setErrorType(errorType: String?) { this.errorType = errorType }

    fun getProcessDetails(): String? = processDetails
    fun setProcessDetails(processDetails: String?) { this.processDetails = processDetails }

    fun getDeviceInfo(): String? = deviceInfo
    fun setDeviceInfo(deviceInfo: String?) { this.deviceInfo = deviceInfo }

    fun getNetworkType(): String? = networkType
    fun setNetworkType(networkType: String?) { this.networkType = networkType }

    fun getAppVersion(): String? = appVersion
    fun setAppVersion(appVersion: String?) { this.appVersion = appVersion }

    fun getWorkerId(): String? = workerId
    fun setWorkerId(workerId: String?) { this.workerId = workerId }

    // 从BaseEntity继承的字段的Getter和Setter
    fun getId(): Long = id
    fun setId(id: Long) { this.id = id }

    fun getCreateDate(): LocalDateTime? = createDate
    fun setCreateDate(createDate: LocalDateTime?) { this.createDate = createDate }

    fun getUpdateDate(): LocalDateTime? = updateDate
    fun setUpdateDate(updateDate: LocalDateTime?) { this.updateDate = updateDate }

    fun getDelAt(): LocalDateTime? = delAt
    fun setDelAt(delAt: LocalDateTime?) { this.delAt = delAt }

    fun getDelFlag(): Int? = delFlag
    fun setDelFlag(delFlag: Int?) { this.delFlag = delFlag }
    fun setDelFlag(delFlag: Byte) { this.delFlag = delFlag.toInt() }

    companion object {
        /**
         * 处理类型：OCR识别
         */
        const val PROCESS_TYPE_OCR = 1
        
        /**
         * 处理类型：图片存储
         */
        const val PROCESS_TYPE_IMAGE_STORAGE = 2
        
        /**
         * 处理类型：缩略图生成
         */
        const val PROCESS_TYPE_THUMBNAIL = 3
        
        /**
         * 处理类型：信息提取
         */
        const val PROCESS_TYPE_INFO_EXTRACTION = 4
        
        /**
         * 处理状态：待处理
         */
        const val STATUS_PENDING = 1
        
        /**
         * 处理状态：处理中
         */
        const val STATUS_PROCESSING = 2
        
        /**
         * 处理状态：已完成
         */
        const val STATUS_COMPLETED = 3
        
        /**
         * 处理状态：失败
         */
        const val STATUS_FAILED = 4
    }
} 